package com.example.demo1.controller;


import com.example.demo1.pojo.Article;
import com.example.demo1.pojo.Category;
import com.example.demo1.pojo.PageBean;
import com.example.demo1.pojo.Result;
import com.example.demo1.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result<Integer> add(@RequestBody @Validated Article article){
        Integer articleid = articleService.add(article);
        return Result.success(articleid);
    }

    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum,Integer pageSize,@RequestParam(required = false) Integer categoryId,@RequestParam(required = false) String state, @RequestParam(required = false) String content){
        System.out.println(pageNum+" "+state+" "+categoryId+" "+content);
//        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);
        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state,content);
        System.out.println(pb.toString());
        return Result.success(pb);
    }

    @PostMapping("/upload")
    public Result<String> upload(@RequestBody @RequestParam(value = "file", required = false) MultipartFile file, Integer id){
        //System.out.println(file.getName());
        if(file==null){
            return Result.success();
        }
        String pathFile = articleService.upload(file, id);
        return Result.success(pathFile);
    }

    @PutMapping
    public Result<Integer> update(@RequestBody @Validated Article article){
        Integer articleid = articleService.update(article);
        return Result.success(articleid);
    }

    @DeleteMapping
    public Result delete(Integer id){
        articleService.deleteById(id);
        return Result.success();
    }
}
